package cn.tedu.travelsystem.orderItem.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.orderItem.mapper.OrderItemMapper;
import cn.tedu.travelsystem.orderItem.pojo.dto.OrderItemaddParam;
import cn.tedu.travelsystem.orderItem.pojo.entity.OrderItem;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.tedu.travelsystem.base.response.JsonResult.ok;

@Validated
@Slf4j
@Api(tags = "10.订单项目模块")
@RestController
@RequestMapping("/orderitem")
public class OrderItemController {
    @Autowired
    private OrderItemMapper orderItemMapper;
    @PostMapping("insertOrderItem")
    @ApiOperation("订单项目插入")
    @ApiOperationSupport(order = 10)
    public JsonResult insertOrder(@Validated @RequestBody OrderItemaddParam orderItemaddParam) {
        log.debug("OrderItemaddParam = " +orderItemaddParam);
        OrderItem orderitem = new OrderItem();
        BeanUtils.copyProperties(orderItemaddParam,orderitem);
         // 使用 java.util.Date 类
        orderItemMapper.insertOrderItem(orderitem);

        return ok();
    }
    @ApiOperationSupport(order = 50)
    @ApiOperation("删除订单项功能")
    @DeleteMapping("delete/{itemId}") // 改成路径参数
    @ResponseBody
    public String delete(@PathVariable Long itemId) {
        System.out.println(itemId);
        int rows = orderItemMapper.deleteOrderItemById(itemId); // 执行删除并返回影响行数
        if (rows > 0) {
            return "删除成功";
        } else {
            return "删除失败，用户不存在或已被删除";
        }
    }
    @ApiOperation("查询订单详情")
    @ApiOperationSupport(order = 30)
    @GetMapping("/get/{itemId}")
    public JsonResult getOrderItem(@PathVariable Long itemId) {
        OrderItem orderItem = orderItemMapper.getOrderItemById(itemId);
        if (orderItem != null) {
            return JsonResult.ok(orderItem);
        } else {
            return JsonResult.ok("订单不存在");
        }
    }

}

